<?php
/*
 * 采购列表页筛选
 * @author     Mark <zhaody901@126.com>
 * @copyright  Copyright (c) 2013 lemon team
 * @version    $Id: 47c0e3a81174dabac1a8e0ed95a61e1f3bb92f6d $
 * @time       13-12-27 下午1:56
 */
defined('IN_IXDCW') or exit('Access Denied');
require DT_CORE . '/' . MODULE_NAME . '/common.inc.php';

// 增加url自动生成
$filter = array();
//获取筛选的参数值
// normal
$filter['catid']  = getgpc('catid');
$filter['areaid'] = getgpc('areaid');
$filter['typeid'] = getgpc('typeid');

// auto
$filter['ctp'] = getgpc('ctp');
$filter['bid'] = getgpc('bid');
$filter['cid'] = getgpc('cid');
$filter['mid'] = getgpc('mid');
$filter['cen'] = getgpc('cen');
//获取类型。
$TYPES = explode('|', $MOD['type']);
//信息搜索页面调用车型的搜索信息
require_once DT_INC . '/search/listAuto.inc.php';

$maincat = Search::mainCat(MODULE_NAME, $filter['catid']);


if (!empty($ARE)) {
	$mainarea = Xd_get_arealists($ARE);
} else {
	$mainarea[0] = get_mainarea(0);
}

$areaParentid = $ARE['parentid'];

if ($kw) {
	SearchCheck::length($kw);
	SearchCheck::timeLimit();
}

// condition
$condition = "(status=3 or status=2)";

if ($filter['areaid']) {
	$condition .= $ARE['child'] ? " AND areaid IN (" . $ARE['arrchildid'] . ") " : " AND areaid={$filter['areaid']} ";
}
//搜索
if ($kw) {
	$condition .= " AND title LIKE '%$kw%'";
}
//文章分类
if (!empty($CAT)) {
	$condition .= ($CAT['child']) ? " AND catid IN (" . $CAT['arrchildid'] . ")" : " AND catid=$catid ";
}
if ($filter['typeid'] && array_key_exists($filter['typeid'], $TYPES)) {
	$condition .= " AND typeid={$filter['typeid']} ";
}

if ($cityid && $cityid != 0) {
	$city_order = '(case when areaid IN (' . $AREA[$cityid]["arrchildid"] . ') then 1 else 2 end ),';
	$push_city_order='(case when city like "%|'.$cityid.'|%" or city like "%|0|%" then 1 else 2 end ),';
} else {
	$city_order = '';
}

if ($autoFilter) {

	$carids   = array_unique((array) __($MODELS)->pluck('arcid'));
	$brandids = array_unique((array) __($MODELS)->pluck('arbid'));
	$modelids = array_unique((array) __($MODELS)->pluck('amid'));

	$carids   = $carids ? $carids : array('-');
	$brandids = $brandids ? $brandids : array('-');
	$modelids = $modelids ? $modelids : array('-');

	$condition .= " and amid in (" . Sql::in($modelids) . ")";
	$condition .= " and arcid in (" . Sql::in($carids) . ")";
	$condition .= " and arbid in (" . Sql::in($brandids) . ")";

}

// order

$ORDER = array($MOD['order'], /*'groupid desc', 'groupid asc', */
	'addtime DESC' /*, 'addtime ASC'*/);
isset($ORDER[$order]) or $order = 0;

// page
$total = $db->count($table, $condition, $DT['cache_search']);


$do = new PurchaseModule($moduleid);

if ($total) {
	$tags = $do->getList($condition, $push_city_order . $ORDER[$order], ($DT['cache_search'] && $page == 1) ? 'CACHE' : '', $DT['cache_search']);
    if ($page == 1 && $kw) keyword($kw, $items, $moduleid);
}

foreach ($tags as $v => $k) {
	$username           = $k;
	$user               = userinfo($username['username']);
	$online             = online($user['userid']);
	$tags[$v]['online'] = $online;
}

$seo_file = 'list';
include DT_INC . '/seo.inc.php';

$template = $CAT['template'] ? $CAT['template'] : 'list';
include template($template, MODULE_NAME);